package LeetCode;

import java.util.Stack;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}15:11
 */
public class LeetCode20 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        char[] chars = s.toCharArray();
        for (char c : chars) {
            if (c == '(' || c == '{' || c == '['){
                stack.push(c);
            }else {
                if (!stack.isEmpty()){
                    if (c == ')' && stack.peek() == '('){
                        stack.pop();
                    }else if (c == '}' && stack.peek() == '{'){
                        stack.pop();
                    }else if (c == ']' && stack.peek() == '['){
                        stack.pop();
                    }else {
                        stack.push(c);
                    }
                }else {
                    return false;
                }
            }
        }
        return stack.isEmpty() ? true : false;
    }


    public static void main(String[] args) {
        System.out.println(new LeetCode20().isValid("(])"));
    }
}
